package com.google.code.sms.android.parser;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.google.code.sms.android.domain.MediaFolder;
import com.google.code.sms.android.domain.Playlist;
import com.google.code.sms.android.util.ProgressListener;

import android.content.Context;
import com.google.code.sms.android.R;

/**
 * @author Sindre Mehus
 */
public class MediaFoldersParser extends AbstractParser {

    public MediaFoldersParser(Context context) {
        super(context);
    }

    public List<MediaFolder> parse(Reader reader, ProgressListener progressListener) throws Exception {

        updateProgress(progressListener, R.string.parser_reading);
        init(reader);

        List<MediaFolder> result = new ArrayList<MediaFolder>();
        int eventType;
        do {
            eventType = nextParseEvent();
            if (eventType == XmlPullParser.START_TAG) {
                String tag = getElementName();
                if ("mediaFolder".equals(tag)) {
                    String id = get("id");
                    String name = get("name");
                    result.add(new MediaFolder(id, name));
                } else if ("error".equals(tag)) {
                    handleError();
                }
            }
        } while (eventType != XmlPullParser.END_DOCUMENT);

        validate();
        updateProgress(progressListener, R.string.parser_reading_done);

        return result;
    }

}